package com.demo.service.system.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dto.Pager;
import com.demo.enums.BizExceptionEnum;
import com.demo.exception.ServiceLayerException;
import com.demo.mapper.system.RoleAdminRelationMapper;
import com.demo.model.system.RoleAdminRelation;
import com.demo.model.system.RoleAdminRelationExample;
import com.demo.service.system.RoleAdminRelationService;

@Service
public class RoleAdminRelationServiceImpl implements RoleAdminRelationService {

	@Autowired
	RoleAdminRelationMapper mapper;
	
	@Override
	@Transactional
	public int add(RoleAdminRelation record) throws ServiceLayerException {
		if (null == record) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}		
		return mapper.insert(record);
	}

	@Override
	@Transactional
	public int update(RoleAdminRelation record) throws ServiceLayerException {
		if (null == record) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}		
		if (null == record || null == record.getId()) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_DTO_ID_NULL);
		}
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	@Transactional
	public int enable(List<Integer> ids) throws ServiceLayerException {
		if (null == ids) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}		
		if (0 == ids.size()) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_LIST_EMPTY);
		}
		RoleAdminRelationExample example = new RoleAdminRelationExample();
		example.or().andIdIn(ids);
		RoleAdminRelation record = new RoleAdminRelation();
		record.setStoreStatus(Boolean.TRUE);
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	@Transactional
	public int disable(List<Integer> ids) throws ServiceLayerException {
		if (null == ids) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}		
		if (0 == ids.size()) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_LIST_EMPTY);
		}
		RoleAdminRelationExample example = new RoleAdminRelationExample();
		example.or().andIdIn(ids);
		RoleAdminRelation record = new RoleAdminRelation();
		record.setStoreStatus(Boolean.FALSE);
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public RoleAdminRelation getById(Integer id) throws ServiceLayerException {
		if (null == id) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}		
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<RoleAdminRelation> findByCondition(RoleAdminRelationExample example) {
		return mapper.selectByExample(example);
	}

	@Override
	public int getCount(RoleAdminRelationExample example) {
		return mapper.countByExample(example);
	}

	@Override
	public Pager<RoleAdminRelation> findByPage(RoleAdminRelationExample example, Pager<RoleAdminRelation> pager) throws ServiceLayerException {
		if (null == pager) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}
		RowBounds rowBounds = new RowBounds(pager.getStart(), pager.getRows());
		pager.setData(mapper.selectByExample(example, rowBounds));
		pager.setTotalRow(mapper.countByExample(example));
		return pager;
	}

	@Override
	@Transactional
	public int addBatch(List<RoleAdminRelation> records) throws ServiceLayerException {
		if (null == records) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_NULL);
		}		
		if (0 == records.size()) {
			throw new ServiceLayerException(BizExceptionEnum.PARAM_LIST_EMPTY);
		}
		return mapper.insertBatch(records);
	}

	@Override
	@Transactional
	public int deleteByCondition(
			RoleAdminRelationExample example) {
		return mapper.deleteByExample(example);
	}

}
